package com.fare.taluo.application.impl.fetl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.fetl.FreightMonitorApplication;
import com.fare.taluo.fetl.domain.entity.FreightMontiorEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.fetl.FreightMonitorVO;
@Transactional
@Scope("singleton")
@Service("freightMonitorApplication")
public class FreightMonitorApplicationImpl implements FreightMonitorApplication{

	public List<FreightMonitorVO> findAll() {
		FreightMontiorEntity entity = null;
		List<FreightMontiorEntity> entityList = null;
		List<FreightMonitorVO> voList = null;
		try {
			entity = new FreightMontiorEntity();
		    entityList = entity.findAll();
			 voList = this.listToVo(entityList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return voList;
		
	}
	public List<com.fare.taluo.vo.fetl.FreightMonitorVO> findByConditions(String fromCity,String toCity,String grabChannelKey,String flightNo,String flightDate){
		FreightMontiorEntity entity = null;
		List<FreightMontiorEntity> entityList = null;
		List<FreightMonitorVO> voList = null;
		try {
			entity = new FreightMontiorEntity();
		    entityList = entity.findByConditions(1, fromCity, toCity, grabChannelKey, flightNo, flightDate);
			 voList = this.listToVo(entityList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return voList;
	}
	
	private List<FreightMonitorVO> listToVo(List<FreightMontiorEntity> entityList){
		List<FreightMonitorVO> voList = new ArrayList<FreightMonitorVO>();
		for (FreightMontiorEntity entity : entityList) {
			voList.add(this.converToVO(entity));
		}	
		return voList;
	}
	private FreightMonitorVO converToVO(FreightMontiorEntity entity){
	    FreightMonitorVO vo = new FreightMonitorVO();
		vo.setCabinNum(entity.getCabinNum());
		vo.setFlightDate(entity.getFlightDate());
		vo.setFlightNo(entity.getFlightNo());
		vo.setFromCity(entity.getFromCity());
		vo.setFromCityName(entity.getFromCityName());
		vo.setGrabChannel(entity.getGrabChannel());
		vo.setGrabChannelKey(entity.getGrabChannelKey());
		vo.setGrabDate(entity.getGrabDate());
		vo.setGrabNum(entity.getGrabNum());
		vo.setHigherPrice(entity.getHigherPrice());
		vo.setId(entity.getId());
		vo.setLowerPrice(entity.getLowerPrice());
		vo.setRowKey(entity.getRowKey());
		vo.setToCity(entity.getToCity());
		vo.setToCityName(entity.getToCityName());
		vo.setCabinNumForAdd(entity.getCabinNumForAdd());
		vo.setCabinNumForDel(entity.getCabinNumForDel());
		return vo;
	}
	
	//根据航班号flightNumber、航班日期departDate、创建日期opDate、起飞城市三字码fromCode，到达城市三字码toCode,查找记录。
		@Override
		public List<FreightMonitorVO> findByData(String flightNumber,String departDate,String opDate,String fromCode,String toCode,String airHbaseKey) {
			FreightMontiorEntity entity = new FreightMontiorEntity();
			List<FreightMonitorVO> voList = new ArrayList<FreightMonitorVO>();
			List<FreightMontiorEntity>  entityList = entity.findByData(flightNumber, departDate, opDate, fromCode, toCode,airHbaseKey);
			
			if(entityList !=null && entityList.size() >0){
				for(FreightMontiorEntity entitys:entityList){
					voList.add(this.converToVO(entitys));
					
				}
			}
			
			return voList;
			
		}

		@Override
		public List<FreightMonitorVO> findByDatas(String flightNumber,
				String departDate, String opDate, String fromCode, String toCode,
				String airHbaseKey) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<FreightMonitorVO> voList = new ArrayList<FreightMonitorVO>();
		List<FreightMontiorEntity>  entityList = 
				new FreightMontiorEntity().findByDataForm(flightNumber, departDate, opDate, fromCode, toCode,airHbaseKey);
		if(entityList !=null && entityList.size() >0){
			int grabNum = 1;
			String date = null;
			for(FreightMontiorEntity entitys:entityList){
				FreightMonitorVO vo = new FreightMonitorVO();
				vo.setCabinNum(entitys.getCabinNum());
				vo.setCabinNumForAdd(entitys.getCabinNumForAdd());
				vo.setCabinNumForDel(entitys.getCabinNumForDel());
				Date queryDates=entitys.getFlightDate();
				String queryDatesString=sdf.format(queryDates);
				try {
					vo.setFlightDate(sdf.parse(queryDatesString));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				vo.setFlightNo(entitys.getFlightNo());
				vo.setFromCity(entitys.getFromCity());
				vo.setFromCityName(entitys.getFromCityName());
				vo.setGrabChannel(entitys.getGrabChannel());
				vo.setGrabChannelKey(entitys.getGrabChannelKey());
				vo.setGrabDate(entitys.getGrabDate());
				String tempDate = DateUtils.formatDay(vo.getGrabDate(),"yyyy-MM-dd");
				if(date == null){
					vo.setGrabNum(grabNum);
				}else if(tempDate.equals(date)){
					grabNum++;
					vo.setGrabNum(grabNum);
				}else{
					grabNum = 1;
					vo.setGrabNum(grabNum);
				}
				date = tempDate;
				vo.setHigherPrice(entitys.getHigherPrice());
				vo.setId(entitys.getId());
				vo.setLowerPrice(entitys.getLowerPrice());
				vo.setRowKey(entitys.getRowKey());
				vo.setToCity(entitys.getToCity());
				vo.setToCityName(entitys.getToCityName());
				voList.add(vo);
				}
			}
		return voList;
		}
		
		//pingjie
		private List pingjie(List<FreightMonitorVO> list){
			Map<String,FreightMonitorVO> map=new LinkedHashMap<String,FreightMonitorVO>();
			for (int i = 0; i < list.size(); i++) {
				FreightMonitorVO old=list.get(i);
				//拼接条件
				String key=old.getGrabDate()+"--"+old.getFlightNo();//拼装成一个key
				if(map.get( key )!=null){//判断已经存在同个日期，同个渠道，成功数相同，实际抓取数相同，同一出发城市，同一到到达城市 的数据
					int k=1;
					k++;
					FreightMonitorVO newVo=map.get(key);//将存在的数据取出来
					  //累加
					/*  Long total=nullToLong(old.getTotal(),newVo.getTotal());
					  
					  Long failNum=nullToLong(old.getFailNum(),newVo.getFailNum());
					  Long secondFail=nullToLong(old.getSecondFail(),newVo.getSecondFail());
					  Long thirdFail=nullToLong(old.getThirdFail(),newVo.getThirdFail());
					  Long forthFail=nullToLong(old.getForthFail(),newVo.getForthFail());
					  Long otherFail=nullToLong(old.getOtherFail(),newVo.getOtherFail());
					  Long waiting=nullToLong(old.getWaiting(),newVo.getWaiting());
					  Long starting=nullToLong(old.getStarting(),newVo.getStarting());
					  Long working=nullToLong(old.getWorking(),newVo.getWorking());
					  Long actualNum=nullToLong(old.getActualNum(), newVo.getActualNum());
					  Long successNum=nullToLong(old.getSuccessNum(), newVo.getSuccessNum());
					  Long maxTime=nullToLong(old.getMaxTime(), newVo.getMaxTime());
					  Long minTime=nullToLong(old.getMinTime(), newVo.getMinTime());
					  newVo.setTotal(total);//total在数据库中多加了一遍成功数
					  newVo.setFailNum(failNum);
					  newVo.setSecondFail(secondFail); 
					  newVo.setThirdFail(thirdFail);
					  newVo.setForthFail(forthFail);
					  newVo.setOtherFail(otherFail);
					  newVo.setWaiting(waiting);
					  newVo.setWorking(working);
					  newVo.setStarting(starting);
					  newVo.setMaxTime(maxTime/k);
					  newVo.setMinTime(minTime/k);
					  newVo.setActualNum(actualNum/k);
					  newVo.setSuccessNum(successNum);					  
					  */
					
		              map.put(key,newVo);
		       }else{
		          map.put(key,old);
		       }
			}
			Object s[] = map.keySet().toArray();
			for(int i = 0; i < map.size(); i++) {
				FreightMonitorVO stu=map.get(s[i]);
			}
			List voList = new ArrayList();
			Iterator iter = map.entrySet().iterator();  //获得map的Iterator
			while(iter.hasNext()) {
				Entry entry = (Entry)iter.next();
				voList.add(entry.getValue());
			}
			return voList;
		}
		@Override
		public List<FreightMonitorVO> findMaxGrabNum(String maxDate,
				String flightNos) {
			FreightMontiorEntity entity = null;
			List<FreightMontiorEntity> entityList = null;
			List<FreightMonitorVO> voList = null;
			try {
				entity = new FreightMontiorEntity();
			    entityList = entity.findMaxGrabNum(maxDate,flightNos);
				 voList = this.listToVo(entityList);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return voList;
		}

}
